<template>
  <div class="header-container">
    <transition name="el-fade-in-linear">
      <div v-show="show" style="display: inline-block;">
        <el-select
          ref="searchInput"
          v-model="value"
          filterable
          placeholder="请选择"
          @blur="hiddenSearch"
          @change="changeRouter"
        >
          <el-option
            v-for="item in routerStore.routeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
    </transition>
    <div
      v-if="btnShow"
      class="user-box"
    >
      <div class="gvaIcon gvaIcon-refresh" :class="[reload ? 'reloading' : '']" @click="handleReload" />
    </div>
    <div
      v-if="btnShow"
      class="user-box"
    >
      <div class="gvaIcon gvaIcon-search" @click="showSearch" />
    </div>
    <div
      v-if="btnShow"
      class="user-box"
    >
      <Screenfull class="search-icon" :style="{cursor:'pointer'}" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'HeaderTool',
}
</script>

<script setup>
import Screenfull from '@/views/hall/screenfull/index.vue'
import { emitter } from '@/utils/bus.js'
import { ref, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useHallRouterStore } from '@/store/modules/hallRouter'

const route = useRoute()
const router = useRouter()

const routerStore = useHallRouterStore()

const value = ref('')
const changeRouter = () => {
  const query = {}
  const params = {}
  const name = value.value
  routerStore.routeMap[name]?.parameters &&
      routerStore.routeMap[name]?.parameters.forEach((item) => {
        if (item.type === 'query') {
          query[item.key] = item.value
        } else {
          params[item.key] = item.value
        }
      })
  if (name === route.name) return
  if (name.indexOf('http://') > -1 || name.indexOf('https://') > -1) {
    window.open(name)
  } else {
    router.push({ name: name, query, params })
  }
  value.value = ''
}

const show = ref(false)
const btnShow = ref(true)
const hiddenSearch = () => {
  show.value = false
  setTimeout(() => {
    btnShow.value = true
  }, 500)
}

const searchInput = ref(null)
const showSearch = async() => {
  btnShow.value = false
  show.value = true
  await nextTick()
  searchInput.value.focus()
}

const reload = ref(false)
const handleReload = () => {
  reload.value = true
  emitter.emit('reload')
  setTimeout(() => {
    reload.value = false
  }, 500)
}

</script>
<style scoped lang="scss">

.header-container{
  display: inline-flex;
}

.user-box {
        cursor: pointer;
        margin-right: 24px;
        color: rgba($color: #000000, $alpha: 0.65);
    }
    
.reload{
  font-size: 18px;
}

.reloading{
  animation:turn 0.5s linear infinite;
}
@keyframes turn {
  0%{-webkit-transform:rotate(0deg);}
  25%{-webkit-transform:rotate(90deg);}
  50%{-webkit-transform:rotate(180deg);}
  75%{-webkit-transform:rotate(270deg);}
  100%{-webkit-transform:rotate(360deg);}
}
</style>
